package com.itheima.a07myzipstream;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class ZipStreamDemo2 {
    public static void main(String[] args) throws IOException {
        //熟悉压缩流
        //压缩本质：把文件看成ZipEntry，然后把多个ZipEntry打包成一个压缩包

        //把D:\Java\bbb打包成压缩包

        //创建file对象再写一个压缩方法
        File start = new File("D:\\Java\\a.txt");
        File end = new File("D:\\Java\\");

        //调用方法压缩
        toZip(start,end);
    }
    public static void toZip(File start,File end) throws IOException {
        //创建压缩流对象
        ZipOutputStream zos=new ZipOutputStream(new FileOutputStream(new File(end,"a.zip")));
        //创建ZipEntry表示获取到的文件
        ZipEntry zipEntry = new ZipEntry("a.txt");
        //把ZipEntry放入压缩包
        zos.putNextEntry(zipEntry);
        //把文本内容也搞进去
        //获取文件内容
        FileInputStream fis=new FileInputStream(start);
        int i;
        while ((i=fis.read())!=-1){
            zos.write(i);
        }
        fis.close();
        zos.closeEntry();
        zos.close();
    }
}
